package com.lkd.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.lkd.dto.LoginDto;
import com.lkd.entity.User;
import com.lkd.exception.BusinessException;
import com.lkd.mapper.UserMapper;
import com.lkd.service.UserService;
import com.lkd.vo.LoginUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;

    //用户登录
    @Override
    public LoginUserVo login(LoginDto dto) {
        if (StrUtil.isBlank(dto.getLoginName()) || StrUtil.isBlank(dto.getPassword())) {
            throw new BusinessException("请求参数不正确");
        }
        // 根据username查询用户信息
        User user1 = BeanUtil.toBean(dto, User.class);
        User user = userMapper.selctByUsername(user1.getLoginName());
        if (user == null) {
            throw new BusinessException("用户名不存在");
        }
        // 对用户输入的密码进行加密
//        String enpass = SecureUtil.md5(dto.getPassword());
        // 和数据库存储的密码进行比较,如果不一样, 就抛出异常
        if (!StrUtil.equals(dto.getPassword(), user.getPassword())) {
            throw new BusinessException("密码输入错误");
        }
        LoginUserVo bean = BeanUtil.toBean(user, LoginUserVo.class);
        bean.setUserId(user.getId());
        return bean;
    }
}
